package com.example.api.controller;

import com.example.common.entity.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 添加用户
     */
    @PostMapping
    public boolean addUser(@RequestBody User user) {
        return userService.saveUser(user);
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id) {
        return userService.getUserById(id);
    }

    /**
     * 获取所有用户
     */
    @GetMapping
    public List<User> listUsers() {
        return userService.listUsers();
    }

    /**
     * 获取所有用户并按年龄降序排序
     */
    @GetMapping("/orderByAge")
    public List<User> listUsersOrderByAgeDesc() {
        return userService.listUsersOrderByAgeDesc();
    }

    /**
     * 更新用户
     */
    @PutMapping
    public boolean updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    public boolean deleteUser(@PathVariable Long id) {
        return userService.deleteUser(id);
    }
} 